By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 Inkscape boolean operations in extension.md

View raw Download
text/x-script.python • 1.97 kiB
Python script, ASCII text executable

title: Using boolean operations in an Inkscape extension topics: ["inkscape", "python", "tip"] DATE: 2025-11-22 ---

I've wasted 3 days doing this, so I will at least publish my findings on the web so you don't have to waste another 3 days. I wasn't able to find a good example online and the docs are not well-made (not saying that they should be a priority), so figuring it out does require some code research.

The inkex module provides lots of things, but boolean operations aren't one of them. Thus, you will have to call Inkscape yourself; luckily, inkex does provide that.

Even though they are listed as arguments, no special processing is done to the values, which are still strings.

I leave a minimal example:

import inkex
import io

class UnionTwoPaths(inkex.EffectExtension):
   def effect(self):
       selected = list(self.svg.selection)

       if len(selected) != 2:
           inkex.errormsg("Select exactly two paths!")
           return

       # Make a string of the IDs
       ids = ",".join([elem.get_id() for elem in selected])

       # Call Inkscape to make a union
       union_svg_bytes = inkex.command.inkscape_command(
           self.document,
           select=ids,
           actions="path-union"
       )

       # Send the drawing back
       self.document = inkex.load_svg(union_svg_bytes)


if __name__ == "__main__":
   UnionTwoPaths().run()
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
   <name>Union of two paths</name>
   <id>org.inkscape.boolean.union_two_paths</id>
   <effect>
       <!-- Enable only on path objects (could use 'all' if preferred) -->
       <object-type>path</object-type>
       <effects-menu>
           <submenu name="Generate from Path"/>
       </effects-menu>
   </effect>
   <script>
       <command reldir="extensions" interpreter="python">union_two_paths.py</command>
   </script>
</inkscape-extension>
                
                    
1
---
2
title: Using boolean operations in an Inkscape extension
3
topics: ["inkscape", "python", "tip"]
4
DATE: 2025-11-22
5
---
6
7
I've wasted 3 days doing this, so I will at least publish my findings on the web
8
so you don't have to waste another 3 days. I wasn't able to find a good example
9
online and the docs are not well-made (not saying that they should be a
10
priority), so figuring it out does require some code research.
11
12
The `inkex` module provides lots of things, but boolean operations aren't one of
13
them. Thus, you will have to call Inkscape yourself; luckily, `inkex` does
14
provide that.
15
16
Even though they are listed as arguments, no special processing is done to the
17
values, which are still strings.
18
19
I leave a minimal example:
20
21
~~~python
22
import inkex
23
import io
24
25
class UnionTwoPaths(inkex.EffectExtension):
26
def effect(self):
27
selected = list(self.svg.selection)
28
29
if len(selected) != 2:
30
inkex.errormsg("Select exactly two paths!")
31
return
32
33
# Make a string of the IDs
34
ids = ",".join([elem.get_id() for elem in selected])
35
36
# Call Inkscape to make a union
37
union_svg_bytes = inkex.command.inkscape_command(
38
self.document,
39
select=ids,
40
actions="path-union"
41
)
42
43
# Send the drawing back
44
self.document = inkex.load_svg(union_svg_bytes)
45
46
47
if __name__ == "__main__":
48
UnionTwoPaths().run()
49
~~~
50
51
~~~xml
52
<?xml version="1.0" encoding="UTF-8"?>
53
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
54
<name>Union of two paths</name>
55
<id>org.inkscape.boolean.union_two_paths</id>
56
<effect>
57
<!-- Enable only on path objects (could use 'all' if preferred) -->
58
<object-type>path</object-type>
59
<effects-menu>
60
<submenu name="Generate from Path"/>
61
</effects-menu>
62
</effect>
63
<script>
64
<command reldir="extensions" interpreter="python">union_two_paths.py</command>
65
</script>
66
</inkscape-extension>
67
~~~
68